package com.example.shengsheng.activity;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;

import com.example.shengsheng.R;
import com.example.shengsheng.adapter.StringListRecyclerAdpater;

import java.util.ArrayList;

public class RecyclerView1Activity extends AppCompatActivity {

    private RecyclerView stringListRecyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view1);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        // 这里没有设置 setHomeButtonEnabled(true); 当然有下面的方法 onOptionsItemSelected，也实现了显示返回箭头，点击实现返回的功能。
        // getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        // 或者这样
        ActionBar actionBar = this.getSupportActionBar();
        // 下面添加了 if 判断才是完美的做法
        if (actionBar != null){
            //actionBar.setTitle("哎呀呀呀");
            // 隐藏 app:title；
            actionBar.setDisplayShowTitleEnabled(false);
            // 没有下面这一行，也没影响；
            //actionBar.setHomeButtonEnabled(true);
            // 下面这一行必须有，否则，不显示返回箭头，点击那个区域也没有反应；
            actionBar.setDisplayHomeAsUpEnabled(true);
        }


        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        this.stringListRecyclerView = findViewById(R.id.stringListRecyclerView);

        this.stringListRecyclerView.setHasFixedSize(true);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        this.stringListRecyclerView.setLayoutManager(linearLayoutManager);

        ArrayList<String> stringArrayList = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            stringArrayList.add("string-" + i);
        }

        StringListRecyclerAdpater adapter = new StringListRecyclerAdpater(this, stringArrayList);
        this.stringListRecyclerView.setAdapter(adapter);
    }

    // activity 类中的方法
    // 添加点击返回箭头事件
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == android.R.id.home)
        {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
